﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

using FrbaBus.Core;

namespace FrbaBus.Abm_Recorrido
{
    public partial class RecorridoModificacion1 : Form
    {
        public RecorridoModificacion1()
        {
            InitializeComponent();
        }

        public bool origen = false;
        public Form1 parent = null;
        public RecorridoModificacion1(Form callingForm)
        {
            parent = callingForm as Form1;

            InitializeComponent();
        }

        private void RecorridoModificacion1_Load(object sender, EventArgs e)
        {
            comboBox3.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            textBox5.Visible = false;
            textBox6.Visible = false;
            button1.Visible = false;
            button2.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            label7.Visible = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        }

        private void checkBoxBusqAvanzada_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxBusqAvanzada.Checked == false)
            {
                comboBox3.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
                textBox4.Visible = false;
                textBox5.Visible = false;
                textBox6.Visible = false;
                button1.Visible = false;
                button2.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
                label5.Visible = false;
                label6.Visible = false;
                label7.Visible = false;
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                comboBox3.Text = "";
                dataGridView1.Rows.Clear();

            }
            else if (checkBoxBusqAvanzada.Checked == true)
            {
                comboBox3.Visible = true;
                textBox2.Visible = true;
                textBox3.Visible = true;
                textBox4.Visible = true;
                textBox5.Visible = true;
                textBox6.Visible = true;
                button1.Visible = true;
                button2.Visible = true;
                label2.Visible = true;
                label3.Visible = true;
                label4.Visible = true;
                label5.Visible = true;
                label6.Visible = true;
                label7.Visible = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            origen = true;
            Abm_Recorrido.BusquedaCiudad frm = new Abm_Recorrido.BusquedaCiudad(this);
            frm.ShowDialog(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            origen = false;
            Abm_Recorrido.BusquedaCiudad frm = new Abm_Recorrido.BusquedaCiudad(this);
            frm.ShowDialog(this);
        }

        private void RecorridoModificacion1_Activated(object sender, EventArgs e)
        {

            comboBox3.Items.Clear();
            comboBox3.Text = "";
            DBManager db = new DBManager();
            SqlCommand consulta = new SqlCommand("select Tipo_Servicio from ALL_IN.Tipo_Servicio");
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select count (*) from ALL_IN.Tipo_Servicio");
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                comboBox3.Items.Add(r.result.Rows[i][0].ToString());
            }
        }

        public class Foo
        {
            public static int validacion = 1;
        }

        // CONTROLES DE INGRESOS DE DATOS EN TEXTBOX NUMERICOS
        private void Numerical_TextBox_Check(object sender, KeyPressEventArgs e)
        {
            // only allow numerical digits
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            // oly allow 2 decimal digits
            if (Regex.IsMatch(textBox5.Text, @"\.\d\d") && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void Numerical_TextBox_Check2(object sender, KeyPressEventArgs e)
        {
            // only allow numerical digits
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            // oly allow 2 decimal digits
            if (Regex.IsMatch(textBox2.Text, @"\.\d\d") && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void addpathern()
            {
                if ( first == true )
                {
                    first = false;
                    query = query + " where";
                }
                else
                    query = query + " AND";
            }
        public bool first;
        public string query = null;

        private void btnBuscar_Click(object sender, EventArgs e)
        {

            query = " from ALL_IN.Recorrido";
            DBManager db = new DBManager();
            first = true;
            dataGridView1.Rows.Clear();
            if (checkBoxBusqAvanzada.Checked == true)
            {
                if ( textBox3.Text != "")
                {
                    this.addpathern();
                    query = query + " Origen='" + textBox3.Text + "'";
                }
                if (textBox4.Text != "")
                {
                   addpathern();
                   query = query + " Destino='" + textBox4.Text + "'";
                }
                if (textBox5.Text != "")
                {
                    addpathern();
                    query = query + " Precio_Pasaje_Base='" + textBox5.Text + "'";
                }
                if (textBox2.Text != "")
                {
                    addpathern();
                    query = query + " Precio_Kg_Base='" + textBox2.Text + "'";
                }
                if (textBox6.Text != "")
                {
                    addpathern();
                    query = query + " ID_Recorrido='" + textBox6.Text + "'";
                }
                if (comboBox3.Text != "")
                {
                    addpathern();
                    query = query + " Tipo_Servicio='" + comboBox3.Text + "'";
                }
            }
            if (textBox1.Text != "")
            {
                    addpathern();
                    query = query + " ( Origen like '%" + textBox1.Text + "%' OR Destino like '%" + textBox1.Text + 
                        "%' OR Precio_Pasaje_Base like '%" + textBox1.Text + "%' OR Precio_Kg_Base like '%" + textBox1.Text
                        +  "%' OR ID_Recorrido like '%" + textBox1.Text + "%' OR Tipo_Servicio like '%" + textBox1.Text + "%' )";
            }
            addpathern();
            query = query + " Habilitado=1";
            SqlCommand consulta = new SqlCommand("select *" + query);
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select count (*)" + query);
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                dataGridView1.Rows.Add(r.result.Rows[i][0].ToString(), r.result.Rows[i][1].ToString(), r.result.Rows[i][2].ToString(),
                    r.result.Rows[i][3].ToString(), r.result.Rows[i][4].ToString(), r.result.Rows[i][5].ToString());
            }



        }

        public DataGridViewRow Row;
        
        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                Row = dataGridView1.SelectedRows[0];


                if (parent.baja == false)
                {
                    Abm_Recorrido.RecorridoModificacion2 frm = new Abm_Recorrido.RecorridoModificacion2(this);
                    frm.ShowDialog(this);
                    this.Close();
                }
                else if (parent.baja == true)
                {
                    DialogResult dialogResult = MessageBox.Show("¿Esta seguro que desea eliminarlo", "Eliminar", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        DBManager db = new DBManager();

                        SqlCommand consulta1 = new SqlCommand("DELETE from ALL_IN.viajesActivos WHERE ID_Recorrido = @ID_Recorrido");
                        consulta1.Parameters.Add("@ID_Recorrido", SqlDbType.NVarChar)
                        .Value = String.Copy(this.Row.Cells[0].Value.ToString());
                        SQLResponse r1 = db.executeQuery(consulta1);


                        SqlCommand consulta2 = new SqlCommand("UPDATE ALL_IN.Recorrido set Habilitado=0 WHERE ID_Recorrido=@ID_Recorrido");
                        consulta2.Parameters.Add("@ID_Recorrido", SqlDbType.NVarChar)
                        .Value = String.Copy(this.Row.Cells[0].Value.ToString());
                        SQLResponse r2 = db.executeQuery(consulta2);

                        this.Close();
                    }
                    else if (dialogResult == DialogResult.No)
                    {

                    }
                }

            }
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '0' || e.KeyChar != '1' || e.KeyChar != '2' ||e.KeyChar != '3' ||e.KeyChar != '4' ||e.KeyChar != '5' ||e.KeyChar != '6' ||e.KeyChar != '7' ||e.KeyChar != '8' ||e.KeyChar != '9')
            {
                e.Handled = true;
            }
        }





    }
}
